package com.qf.mybatis01.jdbc;

import com.qf.mybatis01.entity.User;

import java.sql.*;

public class JdbcDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://49.232.214.218:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false";

        //2. 创建 Connection 对象
        final Connection connection = DriverManager.getConnection(url, "root", "root");

        //3. 创建 PreStatement 对象

        String sql = "select * from t_users where id > ?";

        final PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setInt(1,1);

        //4. 执行 sql
        final ResultSet rs = preparedStatement.executeQuery();

        //5. 遍历 结果集
        while (rs.next()) {
            // 手工 ORM
            final User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));
            user.setSex(rs.getString("sex"));
            System.out.println(user);
        }

        //6. 关闭对象
        rs.close();
        preparedStatement.close();
        connection.close();


    }
}
